package com.lazy.es.common;

import java.util.concurrent.CountDownLatch;

/**
 * 并发线程运行的task, 当所有启动线程结束后, 继续, 任务
 * @author luodan
 *
 */
public class LatchTask implements Runnable {
    private CountDownLatch latch;
    private LatchWorker worker;
    private int flag; // 做一些特殊的处理, 这里是做参数校验
    
    public LatchTask(CountDownLatch latch, LatchWorker worker, int flag){
        this.latch = latch;
        this.worker = worker;
        this.flag = flag;
    }

    @Override
    public void run(){
        try{
            worker.work();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            latch.countDown();
        }
    }

    public static interface LatchWorker {
        public void work();
    }
}
